﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Mvc;
using SDT.Model;
using SDT.Lib.Paging;
using SDT.Site.ViewModels;
using SDT.Site.Properties;
using System.Threading;
using System.Globalization;

namespace SDT.Site.Controllers
{
    public class QuestionController : Controller
    {
        SDTDataContext eDataContext = new SDTDataContext();

        public QuestionController()
        {
            var currentCulture = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
        }

        //
        // GET: /Question/

        public ActionResult Index(int? page)
        {
            page = page.HasValue && page.Value > 0 ? page.Value : 1;
            string order = "AddDate desc";
            IPagedList<Question> que = eDataContext.Questions
                                      .Where(p => p.PubYN == true)
                                      .OrderBy(string.Format("{0}", order))
                                      .ToPagedList(page.Value - 1, Settings.Default.ProductPageSize);

            return View(new QuestionViewModels
            {
                Questions = que
            });
        }

        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 19/07/2011
        /// </summary>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult Lasted()
        {
            string order = "nViews desc";
            List<Question> Que = eDataContext.Questions
                          .Where(c => c.PubYN == true)
                          .OrderBy(string.Format("{0}", order))
                          .ToPagedList(Settings.Default.IntDefault, Settings.Default.nItemRightSite).ToList();

            return View(Que);
        }

        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 19/07/2011
        /// </summary>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult Newsest()
        {
            string order = "AddDate desc";
            List<Question> pro = eDataContext.Questions
                          .Where(c => c.PubYN == true)
                          .OrderBy(string.Format("{0}", order))
                          .ToPagedList(Settings.Default.IntDefault, Settings.Default.nItemRightSite).ToList();

            return View(pro);
        }

        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 20/07/2011
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create()
        {
            List<ArticleCategory> cate = eDataContext.ArticleCategories.Where(c => c.ActiveYN == true).ToList();
            ViewData["questionContent"] = Request["questionContent"];
            Question que = new Question();
            que.QuestionContent = Request["questionContent"];

            return View(new QuestionInputModels()
            {
                ArticleCategory = cate,
                Question = que
            });
        }

        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 20/07/2011
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CreateQuestion(QuestionInputModels m)
        {
            try
            {
                UpdateModel(m.Question);
                m.Question.AddDate = DateTime.Now;
                eDataContext.Questions.InsertOnSubmit(m.Question);
                eDataContext.SubmitChanges();

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                List<QuestionCategory> cate = eDataContext.QuestionCategories.Where(c => c.ActiveYN == true).ToList();

                return View(new QuestionInputModels()
                {
                    QuestionCategory = cate,
                    Question = m.Question
                });
            }
        }


        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 20/07/2011
        /// </summary>
        /// <returns></returns>
        public ActionResult Details(int id, int? page)
        {
            Question que = eDataContext.Questions.Where(q => q.ID == id).FirstOrDefault();

            string order = "AddDate desc";
            page = page.HasValue ? page.Value : 1;
            IPagedList<Answer> listA = eDataContext.Answers
                                .Where(a => a.QuestionID == id && a.PubYN.HasValue && a.PubYN.Value == true)
                                .OrderBy(string.Format("{0}", order))
                                .ToPagedList(page.Value - 1, Settings.Default.AnswerPageSize);

            // increment nViews
            que.nViews = que.nViews + 1;
            eDataContext.SubmitChanges();

            return View(new QuestionInputModels()
            {
                Question = que,
                ListAnswer = listA
            });
        }

        /// <summary>
        /// Author: Takamaru Nguyen
        /// Date: 21/07/2011
        /// </summary>
        [ValidateInput(false)]
        public ActionResult Answer(QuestionInputModels m)
        {
            UpdateModel(m);
            m.Answer.AddDate = DateTime.Now;
            m.Answer.PubYN = false;

            // increment nAnswers
            Question q = eDataContext.Questions.Where(qu => qu.ID == m.Answer.QuestionID).FirstOrDefault();
            q.nAnswers = q.nAnswers + 1;

            // insert answer
            eDataContext.Answers.InsertOnSubmit(m.Answer);
            eDataContext.SubmitChanges();

            return RedirectToAction("Index");
        }
    }
}
